---------------------------------------------------------------------------------
      name:  <unnamed>
       log:  /Users/tomasdomingueziino/Dropbox/Research/jmp/current/data_new/shap
> efiles/raw/Ehrl_AMCgeneration_EE/_Crosswalk_1991_2010_1991.log
  log type:  text
 opened on:   8 Apr 2021, 10:37:55
  123
   19
    6
    0
(85 missing values generated)
(85 missing values generated)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(0 real changes made)
(5592 missing values generated)
(1010 real changes made)
(0 real changes made)
(1010 real changes made)

. 
. *******************************************
. **** Auxiliary file 2: "matching.do"   ****
. **** matching of cluster-numbers       ****
. *******************************************
. **** Ehrl (2017) Estudos Economicos 47/1 ** 
. *******************************************
. 
. *** save the current data in use (to restore it later)
. save "current.dta", replace
(note: file current.dta not found)
file current.dta saved

. 
. *** keep only the two different group numbers
. keep clu`1' clu_new

. keep if clu_new!=.
(4582 observations deleted)

. sort clu`1' clu_new

. ** drop unecessary obs:
. drop if clu`1'==clu_new
(1010 observations deleted)

. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
(0 observations deleted)

. 
. *** 1. check
. *** problem for several destinies (to be replaced by only 1 new clu-number)
.  count if clu`1'==clu`1'[_n-1]
    0

.  while r(N)!=0 {
  2.         replace clu`1'=clu_new if clu`1'==clu`1'[_n-1]
  3.         replace clu_new=clu_new[_n-1] if clu_new==clu`1'
  4.         sort clu`1' clu_new
  5.         // drop double obs. (in case)
.         drop if clu`1'==clu_new
  6.         drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
  7.         // to end the loop:
.         count if clu`1'==clu`1'[_n-1]
  8. }

.  save "temp.dta", replace
(note: dataset contains 0 observations)
(note: file temp.dta not found)
file temp.dta saved

.  
.  *** 2. check if a problem will occur:
.  *** problem for cross-ref (a new cluster number is at the same time replaced b
> y another new one)
.  preserve 

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    0

. // if yes:
.  if r(N)!=0 {
. restore
. 
. rename clu_new clu_new2 
. rename clu`1' clu_new
. save "temp2.dta", replace
. use "temp.dta", clear
. merge m:1 clu_new using "temp2.dta", nogen
. replace clu_new2=clu_new if clu_new2==.
. keep if clu`1'!=.
. drop clu_new
. rename clu_new2 clu_new 
. sort clu`1' clu_new
. // drop unecessary obs:
. drop if clu`1'==clu_new
. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
. save "temp.dta", replace
. }

. // if no:
. else {
. restore
. }

. 
. *** repeat this procedure several times
. 
. *** 1. check:
. count if clu`1'==clu`1'[_n-1]
    0

. while r(N)!=0 {
  2.         replace clu`1'=clu_new if clu`1'==clu`1'[_n-1]
  3.         replace clu_new=clu_new[_n-1] if clu_new==clu`1'
  4.         sort clu`1' clu_new
  5.         // drop double obs. (in case)
.         drop if clu`1'==clu_new
  6.         drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
  7.         // to end the loop:
.         count if clu`1'==clu`1'[_n-1]
  8. }

.  
. *** 2. check if a problem will occur
.  preserve 

.  cap drop help

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    0

. // if yes:
.  if r(N)!=0 {
.  restore
. 
. rename clu_new clu_new2 
. rename clu`1' clu_new
. save "temp2.dta", replace
. use "temp.dta", clear
. merge m:1 clu_new using "temp2.dta", nogen
. replace clu_new2=clu_new if clu_new2==.
. keep if clu`1'!=.
. drop clu_new
. rename clu_new2 clu_new 
. sort clu`1' clu_new
. // drop unecessary obs:
. drop if clu`1'==clu_new
. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
. save "temp.dta", replace
. }

. // if no
. else {
. restore
. }

. 
. *** 1. check:
. count if clu`1'==clu`1'[_n-1]
    0

. while r(N)!=0 {
  2.         replace clu`1'=clu_new if clu`1'==clu`1'[_n-1]
  3.         replace clu_new=clu_new[_n-1] if clu_new==clu`1'
  4.         sort clu`1' clu_new
  5.         // drop double obs. (in case)
.         drop if clu`1'==clu_new
  6.         drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
  7.         // to end the loop:
.         count if clu`1'==clu`1'[_n-1]
  8. }

.  
. *** 2. check if a problem will occur
.  preserve 

.  cap drop help

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    0

. // if yes:
.  if r(N)!=0 {
.  restore
. 
. rename clu_new clu_new2 
. rename clu`1' clu_new
. save "temp2.dta", replace
. use "temp.dta", clear
. merge m:1 clu_new using "temp2.dta", nogen
. replace clu_new2=clu_new if clu_new2==.
. keep if clu`1'!=.
. drop clu_new
. rename clu_new2 clu_new 
. sort clu`1' clu_new
. // drop unecessary obs:
. drop if clu`1'==clu_new
. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
. save "temp.dta", replace
. }

. // if no
. else {
. restore
. }

. 
. *** final checks:
. assert clu`1'!=clu`1'[_n-1]
(null assertion)

.  preserve 

.  cap drop help

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    0

.  assert r(N)==0

.  restore

. 
. // procedure is complete. restore data
. use "current.dta", clear

. 
. *** finally, replace cluster number of all mun of one group
. *** with the new number of the other group
. drop clu_new

. merge m:1 clu`1' using "temp.dta", nogen

    Result                           # of obs.
    -----------------------------------------
    not matched                         5,592
        from master                     5,592  
        from using                          0  

    matched                                 0  
    -----------------------------------------

. replace clu`1'=clu_new if clu_new!=.
(0 real changes made)

. drop clu_new

. 
. erase "temp.dta"

. erase "current.dta"

. cap erase "temp2.dta"

. 
end of do-file
missing matches per group #1

   ch_match |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |      5,440       97.28       97.28
          1 |        125        2.24       99.52
          2 |         21        0.38       99.89
          3 |          6        0.11      100.00
------------+-----------------------------------
      Total |      5,592      100.00
  123
(5444 missing values generated)
(5592 missing values generated)
mis1991 was str28 now str32
(5359 real changes made)
(148 real changes made)
(1 real change made)
(0 real changes made)
(146 real changes made)
(0 real changes made)
  148
(148 real changes made)

. 
. *******************************************
. **** Auxiliary file 2: "matching.do"   ****
. **** matching of cluster-numbers       ****
. *******************************************
. **** Ehrl (2017) Estudos Economicos 47/1 ** 
. *******************************************
. 
. *** save the current data in use (to restore it later)
. save "current.dta", replace
(note: file current.dta not found)
file current.dta saved

. 
. *** keep only the two different group numbers
. keep clu`1' clu_new

. keep if clu_new!=.
(5444 observations deleted)

. sort clu`1' clu_new

. ** drop unecessary obs:
. drop if clu`1'==clu_new
(1 observation deleted)

. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
(0 observations deleted)

. 
. *** 1. check
. *** problem for several destinies (to be replaced by only 1 new clu-number)
.  count if clu`1'==clu`1'[_n-1]
   10

.  while r(N)!=0 {
  2.         replace clu`1'=clu_new if clu`1'==clu`1'[_n-1]
  3.         replace clu_new=clu_new[_n-1] if clu_new==clu`1'
  4.         sort clu`1' clu_new
  5.         // drop double obs. (in case)
.         drop if clu`1'==clu_new
  6.         drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
  7.         // to end the loop:
.         count if clu`1'==clu`1'[_n-1]
  8. }
(10 real changes made)
(10 real changes made)
(0 observations deleted)
(0 observations deleted)
    4
(4 real changes made)
(4 real changes made)
(0 observations deleted)
(0 observations deleted)
    0

.  save "temp.dta", replace
(note: file temp.dta not found)
file temp.dta saved

.  
.  *** 2. check if a problem will occur:
.  *** problem for cross-ref (a new cluster number is at the same time replaced b
> y another new one)
.  preserve 

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
   32

. // if yes:
.  if r(N)!=0 {
. restore
. 
. rename clu_new clu_new2 
. rename clu`1' clu_new
. save "temp2.dta", replace
(note: file temp2.dta not found)
file temp2.dta saved
. use "temp.dta", clear
. merge m:1 clu_new using "temp2.dta", nogen

    Result                           # of obs.
    -----------------------------------------
    not matched                           223
        from master                       108  
        from using                        115  

    matched                                39  
    -----------------------------------------
. replace clu_new2=clu_new if clu_new2==.
(108 real changes made)
. keep if clu`1'!=.
(115 observations deleted)
. drop clu_new
. rename clu_new2 clu_new 
. sort clu`1' clu_new
. // drop unecessary obs:
. drop if clu`1'==clu_new
(4 observations deleted)
. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
(0 observations deleted)
. save "temp.dta", replace
file temp.dta saved
. }

. // if no:
. else {
. restore
. }

. 
. *** repeat this procedure several times
. 
. *** 1. check:
. count if clu`1'==clu`1'[_n-1]
    0

. while r(N)!=0 {
  2.         replace clu`1'=clu_new if clu`1'==clu`1'[_n-1]
  3.         replace clu_new=clu_new[_n-1] if clu_new==clu`1'
  4.         sort clu`1' clu_new
  5.         // drop double obs. (in case)
.         drop if clu`1'==clu_new
  6.         drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
  7.         // to end the loop:
.         count if clu`1'==clu`1'[_n-1]
  8. }

.  
. *** 2. check if a problem will occur
.  preserve 

.  cap drop help

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    6

. // if yes:
.  if r(N)!=0 {
.  restore
. 
. rename clu_new clu_new2 
. rename clu`1' clu_new
. save "temp2.dta", replace
file temp2.dta saved
. use "temp.dta", clear
. merge m:1 clu_new using "temp2.dta", nogen

    Result                           # of obs.
    -----------------------------------------
    not matched                           273
        from master                       136  
        from using                        137  

    matched                                 7  
    -----------------------------------------
. replace clu_new2=clu_new if clu_new2==.
(136 real changes made)
. keep if clu`1'!=.
(137 observations deleted)
. drop clu_new
. rename clu_new2 clu_new 
. sort clu`1' clu_new
. // drop unecessary obs:
. drop if clu`1'==clu_new
(0 observations deleted)
. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
(0 observations deleted)
. save "temp.dta", replace
file temp.dta saved
. }

. // if no
. else {
. restore
. }

. 
. *** 1. check:
. count if clu`1'==clu`1'[_n-1]
    0

. while r(N)!=0 {
  2.         replace clu`1'=clu_new if clu`1'==clu`1'[_n-1]
  3.         replace clu_new=clu_new[_n-1] if clu_new==clu`1'
  4.         sort clu`1' clu_new
  5.         // drop double obs. (in case)
.         drop if clu`1'==clu_new
  6.         drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
  7.         // to end the loop:
.         count if clu`1'==clu`1'[_n-1]
  8. }

.  
. *** 2. check if a problem will occur
.  preserve 

.  cap drop help

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    0

. // if yes:
.  if r(N)!=0 {
.  restore
. 
. rename clu_new clu_new2 
. rename clu`1' clu_new
. save "temp2.dta", replace
. use "temp.dta", clear
. merge m:1 clu_new using "temp2.dta", nogen
. replace clu_new2=clu_new if clu_new2==.
. keep if clu`1'!=.
. drop clu_new
. rename clu_new2 clu_new 
. sort clu`1' clu_new
. // drop unecessary obs:
. drop if clu`1'==clu_new
. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
. save "temp.dta", replace
. }

. // if no
. else {
. restore
. }

. 
. *** final checks:
. assert clu`1'!=clu`1'[_n-1]

.  preserve 

.  cap drop help

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    0

.  assert r(N)==0

.  restore

. 
. // procedure is complete. restore data
. use "current.dta", clear

. 
. *** finally, replace cluster number of all mun of one group
. *** with the new number of the other group
. drop clu_new

. merge m:1 clu`1' using "temp.dta", nogen

    Result                           # of obs.
    -----------------------------------------
    not matched                         5,234
        from master                     5,234  
        from using                          0  

    matched                               358  
    -----------------------------------------

. replace clu`1'=clu_new if clu_new!=.
(358 real changes made)

. drop clu_new

. 
. erase "temp.dta"

. erase "current.dta"

. cap erase "temp2.dta"

. 
end of do-file
missing matches per group #2

   ch_match |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |      5,561       99.45       99.45
          1 |         25        0.45       99.89
          2 |          6        0.11      100.00
------------+-----------------------------------
      Total |      5,592      100.00
   19
(5567 missing values generated)
(5592 missing values generated)
mis1991 was str23 now str32
(5482 real changes made)
(25 real changes made)
(1 real change made)
(0 real changes made)
(148 real changes made)
(0 real changes made)
   25
(25 real changes made)

. 
. *******************************************
. **** Auxiliary file 2: "matching.do"   ****
. **** matching of cluster-numbers       ****
. *******************************************
. **** Ehrl (2017) Estudos Economicos 47/1 ** 
. *******************************************
. 
. *** save the current data in use (to restore it later)
. save "current.dta", replace
(note: file current.dta not found)
file current.dta saved

. 
. *** keep only the two different group numbers
. keep clu`1' clu_new

. keep if clu_new!=.
(5567 observations deleted)

. sort clu`1' clu_new

. ** drop unecessary obs:
. drop if clu`1'==clu_new
(4 observations deleted)

. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
(0 observations deleted)

. 
. *** 1. check
. *** problem for several destinies (to be replaced by only 1 new clu-number)
.  count if clu`1'==clu`1'[_n-1]
    1

.  while r(N)!=0 {
  2.         replace clu`1'=clu_new if clu`1'==clu`1'[_n-1]
  3.         replace clu_new=clu_new[_n-1] if clu_new==clu`1'
  4.         sort clu`1' clu_new
  5.         // drop double obs. (in case)
.         drop if clu`1'==clu_new
  6.         drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
  7.         // to end the loop:
.         count if clu`1'==clu`1'[_n-1]
  8. }
(1 real change made)
(1 real change made)
(0 observations deleted)
(0 observations deleted)
    0

.  save "temp.dta", replace
(note: file temp.dta not found)
file temp.dta saved

.  
.  *** 2. check if a problem will occur:
.  *** problem for cross-ref (a new cluster number is at the same time replaced b
> y another new one)
.  preserve 

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    1

. // if yes:
.  if r(N)!=0 {
. restore
. 
. rename clu_new clu_new2 
. rename clu`1' clu_new
. save "temp2.dta", replace
(note: file temp2.dta not found)
file temp2.dta saved
. use "temp.dta", clear
. merge m:1 clu_new using "temp2.dta", nogen

    Result                           # of obs.
    -----------------------------------------
    not matched                            40
        from master                        20  
        from using                         20  

    matched                                 1  
    -----------------------------------------
. replace clu_new2=clu_new if clu_new2==.
(20 real changes made)
. keep if clu`1'!=.
(20 observations deleted)
. drop clu_new
. rename clu_new2 clu_new 
. sort clu`1' clu_new
. // drop unecessary obs:
. drop if clu`1'==clu_new
(0 observations deleted)
. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
(0 observations deleted)
. save "temp.dta", replace
file temp.dta saved
. }

. // if no:
. else {
. restore
. }

. 
. *** repeat this procedure several times
. 
. *** 1. check:
. count if clu`1'==clu`1'[_n-1]
    0

. while r(N)!=0 {
  2.         replace clu`1'=clu_new if clu`1'==clu`1'[_n-1]
  3.         replace clu_new=clu_new[_n-1] if clu_new==clu`1'
  4.         sort clu`1' clu_new
  5.         // drop double obs. (in case)
.         drop if clu`1'==clu_new
  6.         drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
  7.         // to end the loop:
.         count if clu`1'==clu`1'[_n-1]
  8. }

.  
. *** 2. check if a problem will occur
.  preserve 

.  cap drop help

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    0

. // if yes:
.  if r(N)!=0 {
.  restore
. 
. rename clu_new clu_new2 
. rename clu`1' clu_new
. save "temp2.dta", replace
. use "temp.dta", clear
. merge m:1 clu_new using "temp2.dta", nogen
. replace clu_new2=clu_new if clu_new2==.
. keep if clu`1'!=.
. drop clu_new
. rename clu_new2 clu_new 
. sort clu`1' clu_new
. // drop unecessary obs:
. drop if clu`1'==clu_new
. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
. save "temp.dta", replace
. }

. // if no
. else {
. restore
. }

. 
. *** 1. check:
. count if clu`1'==clu`1'[_n-1]
    0

. while r(N)!=0 {
  2.         replace clu`1'=clu_new if clu`1'==clu`1'[_n-1]
  3.         replace clu_new=clu_new[_n-1] if clu_new==clu`1'
  4.         sort clu`1' clu_new
  5.         // drop double obs. (in case)
.         drop if clu`1'==clu_new
  6.         drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
  7.         // to end the loop:
.         count if clu`1'==clu`1'[_n-1]
  8. }

.  
. *** 2. check if a problem will occur
.  preserve 

.  cap drop help

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    0

. // if yes:
.  if r(N)!=0 {
.  restore
. 
. rename clu_new clu_new2 
. rename clu`1' clu_new
. save "temp2.dta", replace
. use "temp.dta", clear
. merge m:1 clu_new using "temp2.dta", nogen
. replace clu_new2=clu_new if clu_new2==.
. keep if clu`1'!=.
. drop clu_new
. rename clu_new2 clu_new 
. sort clu`1' clu_new
. // drop unecessary obs:
. drop if clu`1'==clu_new
. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
. save "temp.dta", replace
. }

. // if no
. else {
. restore
. }

. 
. *** final checks:
. assert clu`1'!=clu`1'[_n-1]

.  preserve 

.  cap drop help

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    0

.  assert r(N)==0

.  restore

. 
. // procedure is complete. restore data
. use "current.dta", clear

. 
. *** finally, replace cluster number of all mun of one group
. *** with the new number of the other group
. drop clu_new

. merge m:1 clu`1' using "temp.dta", nogen

    Result                           # of obs.
    -----------------------------------------
    not matched                         5,476
        from master                     5,476  
        from using                          0  

    matched                               116  
    -----------------------------------------

. replace clu`1'=clu_new if clu_new!=.
(116 real changes made)

. drop clu_new

. 
. erase "temp.dta"

. erase "current.dta"

. cap erase "temp2.dta"

. 
end of do-file
missing matches per group #3

   ch_match |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |      5,580       99.79       99.79
          1 |         12        0.21      100.00
------------+-----------------------------------
      Total |      5,592      100.00
    6
(5586 missing values generated)
(5592 missing values generated)
mis1991 was str22 now str32
(5501 real changes made)
(6 real changes made)
(1 real change made)
(0 real changes made)
(148 real changes made)
(0 real changes made)
    6
(6 real changes made)

. 
. *******************************************
. **** Auxiliary file 2: "matching.do"   ****
. **** matching of cluster-numbers       ****
. *******************************************
. **** Ehrl (2017) Estudos Economicos 47/1 ** 
. *******************************************
. 
. *** save the current data in use (to restore it later)
. save "current.dta", replace
(note: file current.dta not found)
file current.dta saved

. 
. *** keep only the two different group numbers
. keep clu`1' clu_new

. keep if clu_new!=.
(5586 observations deleted)

. sort clu`1' clu_new

. ** drop unecessary obs:
. drop if clu`1'==clu_new
(1 observation deleted)

. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
(0 observations deleted)

. 
. *** 1. check
. *** problem for several destinies (to be replaced by only 1 new clu-number)
.  count if clu`1'==clu`1'[_n-1]
    0

.  while r(N)!=0 {
  2.         replace clu`1'=clu_new if clu`1'==clu`1'[_n-1]
  3.         replace clu_new=clu_new[_n-1] if clu_new==clu`1'
  4.         sort clu`1' clu_new
  5.         // drop double obs. (in case)
.         drop if clu`1'==clu_new
  6.         drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
  7.         // to end the loop:
.         count if clu`1'==clu`1'[_n-1]
  8. }

.  save "temp.dta", replace
(note: file temp.dta not found)
file temp.dta saved

.  
.  *** 2. check if a problem will occur:
.  *** problem for cross-ref (a new cluster number is at the same time replaced b
> y another new one)
.  preserve 

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    0

. // if yes:
.  if r(N)!=0 {
. restore
. 
. rename clu_new clu_new2 
. rename clu`1' clu_new
. save "temp2.dta", replace
. use "temp.dta", clear
. merge m:1 clu_new using "temp2.dta", nogen
. replace clu_new2=clu_new if clu_new2==.
. keep if clu`1'!=.
. drop clu_new
. rename clu_new2 clu_new 
. sort clu`1' clu_new
. // drop unecessary obs:
. drop if clu`1'==clu_new
. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
. save "temp.dta", replace
. }

. // if no:
. else {
. restore
. }

. 
. *** repeat this procedure several times
. 
. *** 1. check:
. count if clu`1'==clu`1'[_n-1]
    0

. while r(N)!=0 {
  2.         replace clu`1'=clu_new if clu`1'==clu`1'[_n-1]
  3.         replace clu_new=clu_new[_n-1] if clu_new==clu`1'
  4.         sort clu`1' clu_new
  5.         // drop double obs. (in case)
.         drop if clu`1'==clu_new
  6.         drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
  7.         // to end the loop:
.         count if clu`1'==clu`1'[_n-1]
  8. }

.  
. *** 2. check if a problem will occur
.  preserve 

.  cap drop help

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    0

. // if yes:
.  if r(N)!=0 {
.  restore
. 
. rename clu_new clu_new2 
. rename clu`1' clu_new
. save "temp2.dta", replace
. use "temp.dta", clear
. merge m:1 clu_new using "temp2.dta", nogen
. replace clu_new2=clu_new if clu_new2==.
. keep if clu`1'!=.
. drop clu_new
. rename clu_new2 clu_new 
. sort clu`1' clu_new
. // drop unecessary obs:
. drop if clu`1'==clu_new
. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
. save "temp.dta", replace
. }

. // if no
. else {
. restore
. }

. 
. *** 1. check:
. count if clu`1'==clu`1'[_n-1]
    0

. while r(N)!=0 {
  2.         replace clu`1'=clu_new if clu`1'==clu`1'[_n-1]
  3.         replace clu_new=clu_new[_n-1] if clu_new==clu`1'
  4.         sort clu`1' clu_new
  5.         // drop double obs. (in case)
.         drop if clu`1'==clu_new
  6.         drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
  7.         // to end the loop:
.         count if clu`1'==clu`1'[_n-1]
  8. }

.  
. *** 2. check if a problem will occur
.  preserve 

.  cap drop help

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    0

. // if yes:
.  if r(N)!=0 {
.  restore
. 
. rename clu_new clu_new2 
. rename clu`1' clu_new
. save "temp2.dta", replace
. use "temp.dta", clear
. merge m:1 clu_new using "temp2.dta", nogen
. replace clu_new2=clu_new if clu_new2==.
. keep if clu`1'!=.
. drop clu_new
. rename clu_new2 clu_new 
. sort clu`1' clu_new
. // drop unecessary obs:
. drop if clu`1'==clu_new
. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
. save "temp.dta", replace
. }

. // if no
. else {
. restore
. }

. 
. *** final checks:
. assert clu`1'!=clu`1'[_n-1]

.  preserve 

.  cap drop help

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    0

.  assert r(N)==0

.  restore

. 
. // procedure is complete. restore data
. use "current.dta", clear

. 
. *** finally, replace cluster number of all mun of one group
. *** with the new number of the other group
. drop clu_new

. merge m:1 clu`1' using "temp.dta", nogen

    Result                           # of obs.
    -----------------------------------------
    not matched                         5,562
        from master                     5,562  
        from using                          0  

    matched                                30  
    -----------------------------------------

. replace clu`1'=clu_new if clu_new!=.
(30 real changes made)

. drop clu_new

. 
. erase "temp.dta"

. erase "current.dta"

. cap erase "temp2.dta"

. 
end of do-file
missing matches per group #4

   ch_match |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |      5,586       99.89       99.89
          1 |          6        0.11      100.00
------------+-----------------------------------
      Total |      5,592      100.00
    0
(5586 missing values generated)
(5592 missing values generated)
mis1991 was str22 now str32
(5501 real changes made)
(6 real changes made)
(153 real changes made)
(0 real changes made)
(6 real changes made)

. 
. *******************************************
. **** Auxiliary file 2: "matching.do"   ****
. **** matching of cluster-numbers       ****
. *******************************************
. **** Ehrl (2017) Estudos Economicos 47/1 ** 
. *******************************************
. 
. *** save the current data in use (to restore it later)
. save "current.dta", replace
(note: file current.dta not found)
file current.dta saved

. 
. *** keep only the two different group numbers
. keep clu`1' clu_new

. keep if clu_new!=.
(5586 observations deleted)

. sort clu`1' clu_new

. ** drop unecessary obs:
. drop if clu`1'==clu_new
(0 observations deleted)

. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
(1 observation deleted)

. 
. *** 1. check
. *** problem for several destinies (to be replaced by only 1 new clu-number)
.  count if clu`1'==clu`1'[_n-1]
    0

.  while r(N)!=0 {
  2.         replace clu`1'=clu_new if clu`1'==clu`1'[_n-1]
  3.         replace clu_new=clu_new[_n-1] if clu_new==clu`1'
  4.         sort clu`1' clu_new
  5.         // drop double obs. (in case)
.         drop if clu`1'==clu_new
  6.         drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
  7.         // to end the loop:
.         count if clu`1'==clu`1'[_n-1]
  8. }

.  save "temp.dta", replace
(note: file temp.dta not found)
file temp.dta saved

.  
.  *** 2. check if a problem will occur:
.  *** problem for cross-ref (a new cluster number is at the same time replaced b
> y another new one)
.  preserve 

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    0

. // if yes:
.  if r(N)!=0 {
. restore
. 
. rename clu_new clu_new2 
. rename clu`1' clu_new
. save "temp2.dta", replace
. use "temp.dta", clear
. merge m:1 clu_new using "temp2.dta", nogen
. replace clu_new2=clu_new if clu_new2==.
. keep if clu`1'!=.
. drop clu_new
. rename clu_new2 clu_new 
. sort clu`1' clu_new
. // drop unecessary obs:
. drop if clu`1'==clu_new
. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
. save "temp.dta", replace
. }

. // if no:
. else {
. restore
. }

. 
. *** repeat this procedure several times
. 
. *** 1. check:
. count if clu`1'==clu`1'[_n-1]
    0

. while r(N)!=0 {
  2.         replace clu`1'=clu_new if clu`1'==clu`1'[_n-1]
  3.         replace clu_new=clu_new[_n-1] if clu_new==clu`1'
  4.         sort clu`1' clu_new
  5.         // drop double obs. (in case)
.         drop if clu`1'==clu_new
  6.         drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
  7.         // to end the loop:
.         count if clu`1'==clu`1'[_n-1]
  8. }

.  
. *** 2. check if a problem will occur
.  preserve 

.  cap drop help

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    0

. // if yes:
.  if r(N)!=0 {
.  restore
. 
. rename clu_new clu_new2 
. rename clu`1' clu_new
. save "temp2.dta", replace
. use "temp.dta", clear
. merge m:1 clu_new using "temp2.dta", nogen
. replace clu_new2=clu_new if clu_new2==.
. keep if clu`1'!=.
. drop clu_new
. rename clu_new2 clu_new 
. sort clu`1' clu_new
. // drop unecessary obs:
. drop if clu`1'==clu_new
. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
. save "temp.dta", replace
. }

. // if no
. else {
. restore
. }

. 
. *** 1. check:
. count if clu`1'==clu`1'[_n-1]
    0

. while r(N)!=0 {
  2.         replace clu`1'=clu_new if clu`1'==clu`1'[_n-1]
  3.         replace clu_new=clu_new[_n-1] if clu_new==clu`1'
  4.         sort clu`1' clu_new
  5.         // drop double obs. (in case)
.         drop if clu`1'==clu_new
  6.         drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
  7.         // to end the loop:
.         count if clu`1'==clu`1'[_n-1]
  8. }

.  
. *** 2. check if a problem will occur
.  preserve 

.  cap drop help

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    0

. // if yes:
.  if r(N)!=0 {
.  restore
. 
. rename clu_new clu_new2 
. rename clu`1' clu_new
. save "temp2.dta", replace
. use "temp.dta", clear
. merge m:1 clu_new using "temp2.dta", nogen
. replace clu_new2=clu_new if clu_new2==.
. keep if clu`1'!=.
. drop clu_new
. rename clu_new2 clu_new 
. sort clu`1' clu_new
. // drop unecessary obs:
. drop if clu`1'==clu_new
. drop if clu`1'==clu`1'[_n-1] & clu_new==clu_new[_n-1]
. save "temp.dta", replace
. }

. // if no
. else {
. restore
. }

. 
. *** final checks:
. assert clu`1'!=clu`1'[_n-1]

.  preserve 

.  cap drop help

.  rename clu_new help

.  rename clu`1' clu_new

.  append using "temp.dta"

.  gsort clu_new -help, mfirst

.  count if clu_new==clu_new[_n+1] & help[_n+1]!=.
    0

.  assert r(N)==0

.  restore

. 
. // procedure is complete. restore data
. use "current.dta", clear

. 
. *** finally, replace cluster number of all mun of one group
. *** with the new number of the other group
. drop clu_new

. merge m:1 clu`1' using "temp.dta", nogen

    Result                           # of obs.
    -----------------------------------------
    not matched                         5,586
        from master                     5,586  
        from using                          0  

    matched                                 6  
    -----------------------------------------

. replace clu`1'=clu_new if clu_new!=.
(6 real changes made)

. drop clu_new

. 
. erase "temp.dta"

. erase "current.dta"

. cap erase "temp2.dta"

. 
end of do-file
missing matches per group

   ch_match |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |      5,592      100.00      100.00
------------+-----------------------------------
      Total |      5,592      100.00
    0
(85 missing values generated)

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
clu1991_fi~l |      5507    2180.393     1301.37          1       4323
  clu1991 was float now int
  clu1991_final was float now int
  clu1991_orig was float now int
  (33,552 bytes saved)
file _Crosswalk_1991.dta saved
      name:  <unnamed>
       log:  /Users/tomasdomingueziino/Dropbox/Research/jmp/current/data_new/shap
> efiles/raw/Ehrl_AMCgeneration_EE/_Crosswalk_1991_2010_1991.log
  log type:  text
 closed on:   8 Apr 2021, 10:37:59
---------------------------------------------------------------------------------
